﻿using NB.MGJ.Common.API;
using NB.MGJ.Common.Utility;
using NB.MGJ.Model;
using NB.MGJ.Service;
using NB.MGJ.Service.Product;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace NB.MGJ.API
{
    /// <summary>
    /// 产品api
    /// nbapi/product
    /// </summary>

    [ApiAuthorizeFilter]
    [ApiHandleError]
    public class ApiMGJProductController : ApiBaseController<LogOnUserModel>
    {
        private ProductBatchService _ProductBatchService;   
        private ProductService _proService;

        public ApiMGJProductController()
        {
             _ProductBatchService = new ProductBatchService();
             _proService=new ProductService();
        }

        #region 产品
          
        [Route("nbapi/product/selectitem")]
        [HttpGet]
        public IHttpActionResult GetProductNameList(int enterpriseId,int? typeId=null)
        {
            return ApiSuccess(_ProductBatchService.GetProductNameList(enterpriseId,typeId));
        }

        [Route("nbapi/product/enterselectitem")]
        [HttpGet]
        public IHttpActionResult GetEnterpriseNameList( int enterpriseId)
        {
            return ApiSuccess(_ProductBatchService.GetEnterpriseNameList(enterpriseId));
        }
        [Route("nbapi/product/showproductinfo")]
        [HttpGet]
        public IHttpActionResult GetProductModel(int proId)
        {
            return ApiSuccess(_proService.GetProModel(proId));
        }
        #endregion

        #region 
        [ApiVisitFilter("productBatch")]
        [Route("nbapi/product/batch/list")]
        [HttpGet]
        public IHttpActionResult ProductBatchList(string Psearch, int pageindex = 1, int pageSize = 10)
        {
            PagerModel pm = new PagerModel();
            pm.Page = pageindex;
            pm.PageSize = pageSize;

            if (string.IsNullOrEmpty(Psearch))
            {
                pm.StrCondition = " ViewDel=0 and EID=" + LogOnUser.EnterpriseId;
            }
            else
            {
                pm.StrCondition = " ViewDel=0 and  BatchName like '%" + Psearch + "%' and EID=" + LogOnUser.EnterpriseId;
            }

            var model = _ProductBatchService.GetPageList(ref pm, Psearch);
            return ApiSuccess(model.PageData);
        }
        #endregion


        [Route("nbapi/productbatch/selectitem")]
        [HttpGet]
        public IHttpActionResult GetProcBatchList(int enterpriseId)
        {
            return ApiSuccess(_ProductBatchService.GetListByEnterId(enterpriseId));
        }


        [Route("nbapi/productbatch/update")]
        [HttpPost]
        public IHttpActionResult UpdateProductBatch([FromBody]Product_Batch model)
        {
            if (model == null)
            {
                return ApiFail("当前无法执行该操作");
            }          
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeLoginName = LogOnUser.LoginName;
            model.EnterpriseId = LogOnUser.EnterpriseId;
            _ProductBatchService.UpdateProductBatch(model);
            return ApiSuccess(true);

        }



        /// <summary>
        /// 产品批次添加
        /// </summary>
        [ApiVisitFilter("AddBatch")]
        [Route("nbapi/productbatch/addbatch")] 
        [HttpPost]
        public IHttpActionResult AddProductBatch([FromBody]L.EntityModel.Product_Batch model)
       {
           if (model == null)
           {
               return ApiFail("当前无法执行该操作");
           }

           model.CreateDate = DateTime.Now;
           model.ChangeLoginID = LogOnUser.UserID;
           model.ChangeLoginName = LogOnUser.LoginName;
           model.ViewDel = 0;
           model.EnterpriseId = LogOnUser.EnterpriseId;
           model.CommonId = 0;
           //model.ProductId = 0;
       
            _ProductBatchService.AddProductBatch(model);
           return ApiSuccess(true);
       }
 
    }
}
